package com.xy.rpcTest;

import com.xy.test.netty.AppClient;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.charset.StandardCharsets;

public class 报文封装 {

    @Test
    public void testMessage() throws IOException {

        ByteBuf message = Unpooled.buffer();
        message.writeBytes("magic".getBytes(StandardCharsets.UTF_8));
        message.writeByte(11);
        message.writeShort(22);
        message.writeInt(33);
        message.writeByte(0);
        message.writeByte(1);
        message.writeByte(2);
        message.writeLong(12345L);
        // 用对象流转化为字节数据
        AppClient appClient = new AppClient();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outputStream);
        oos.writeObject(appClient);
        byte[] bytes = outputStream.toByteArray();
        message.writeBytes(bytes);

        System.out.println(message);

    }

}
